let baseUrl = ""; //请求基础地址
if (process.env.NODE_ENV === 'development') { // 开发环境
	// baseUrl = "https://sso.xcyunfu.com";
	// baseUrl = "http://192.168.0.148:8088"
	// baseUrl = "http://192.168.0.108:8080"
	// baseUrl = "http://192.168.0.243:8088"
	baseUrl = "https://test.xcyunfu.com"
} else { // 生产环境
	// baseUrl = "https://sso.xcyunfu.com";
	// 192.168.0.108:8080
	// baseUrl = "http://192.168.0.243:8088"
	baseUrl = "https://test.xcyunfu.com"
	// baseUrl = "http://192.168.0.108:8080"
}

// 导出
export default (Vue, vm) => {
	/**
	 * 请求对象
	 */
	uni.$request = {
		/**
		 * get 请求
		 * @param  url 请求地址
		 * @param params 请求参数
		 */
		get: async (url = "", params = {}, header = {}) => {
			return await sendRequest({
				type: "GET",
				url,
				params,
				header
			});
		},
		/**
		 * post 请求
		 * @param  url 请求地址
		 * @param params 请求参数
		 */
		post: async (url = "", params = {}, header = {}) => {
			return await sendRequest({
				type: "POST",
				url,
				params,
				header
			});
		},
		/**
		 * put 请求
		 * @param  url 请求地址
		 * @param params 请求参数
		 */
		put: async (url = "", params = {}, header = {}) => {
			return await sendRequest({
				type: "PUT",
				url,
				params,
				header
			});
		},
		/**
		 * delete 请求
		 * @param  url 请求地址
		 * @param params 请求参数
		 */
		delete: async (url = "", params = {}, header = {}) => {
			return await sendRequest({
				type: "DELETE",
				url,
				params,
				header
			});
		},
		/**
		 * post 请求
		 * @param  url 请求地址
		 * @param params 请求参数
		 */
		formData: async (url = "", params = {}, header = {}) => {
			return await sendRequest({
				type: "POST",
				url,
				params,
				header: {
					'content-type': 'application/x-www-form-urlencoded'
				}
			});
		},
		/**
		 * 文件上传
		 */
		upload: async (url = "", params = {}, header = {}) => {
			return await sendRequest({
				type: "UPLOAD",
				url,
				params,
				header
			});
		}
	};

	/**
	 * 发送请求
	 * @param type 请求类型
	 * @param  url 请求地址
	 * @param params 请求参数
	 * 
	 */
	let sendRequest = async ({
		type = "",
		url = "",
		params = {},
		header = {}
	}) => {
		let data = {
			url: baseUrl + url,
			method: type,
			data: params,
			header: header,
		};
		data = beforeIntercept(data);
		if (data.method === "UPLOAD") {
			return afterIntercept_upload(await uni.uploadFile({ //上传文件
				url: data.url,
				filePath: data.data.filePath,
				name: data.data.name,
				formData: params.formData || {},
				header: {
					...data.header,
				}
			}));
		} else return afterIntercept(await uni.request(data));
	}

	/**
	 * 请求拦截器
	 */
	let beforeIntercept = (data = {
		url: "",
		method: "",
		data: {},
		header: {},
	}) => {
		let token = uni.getStorageSync('token');
		if (token) data.header.access_token = token;
		return data;
	}
	/**
	 * 上传图片响应拦截器
	 */
	let afterIntercept_upload = (res) => {
		// console.log(res.errMsg, "请求图片!");
		if (res.errMsg == 'uploadFile:ok') {
			let resData = JSON.parse(res.data)
			// console.log(resData, "resData");
			return resData
		} else if (res.data.code === 200 || res.data.code === 0) {
			// 登录错误
			return res.data
		} else {
			//  这里处理错误请求,例如网络错误等等
			console.log(res, "请求错误11!");
		}

	}

	/**
	 * 响应拦截器
	 */
	let afterIntercept = (res) => {
		if (res.data.code === 200 || res.data.code === 0) {
			// 登录错误
			return res.data
		} else {

			//  这里处理错误请求,例如网络错误等等
			console.log(res, "请求错误!");
		}
		return res.data
	}

}